#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：03_多线程服务器.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/7/6/006 9:39 
"""

from socket import *
from threading import Thread
from multiprocessing import Pool

def recvClinet(newSocket, destAddr):
    try:
        while True:
            recvData = newSocket.recv(1024)
            if len(recvData) > 0:
                print('recv[%s]:%s' % (str(destAddr), recvData))
            else:
                print('[%s]客户端已经关闭' % str(destAddr))
                break
    finally:
        newSocket.close()

if __name__ == '__main__':
    serSocket = socket(AF_INET, SOCK_STREAM)
    serSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serSocket.bind(("",7789))
    serSocket.listen(200)
    try:
        while True:
            print('-----主进程，，等待新客户端的到来------')
            newSocket, destAddr = serSocket.accept()
            print('-----主进程，，接下来负责数据处理[%s]-----' % str(destAddr))
            pool = Pool()
            pool.apply_async(func=recvClinet, args=(newSocket, destAddr))
            pool.close()
            # pool.join()
            # t.join()
            newSocket.close()
    finally:
        serSocket.close()



